package com.ysd.lis.util.qcrule;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class ALRule extends Rule{
    public ALRule(String id, String name, int a, int n, double l) {
        super(id, name, a, n, l);
        this.type = RuleType.AXRule;
    }

    @Override
    public RuleStatement Evaluate(QCResult result) {
        if (result.getResults().size() < a)
        {
            return RuleStatement.DISABLE;
        }

        List<Result> results = result.getResults();
        //var items = result.Results.GetRange(result.Results.Count - A, A);
        List<Result> items = new ArrayList<>();
        for(int i = 0 ;i< a; i++){
            items.add(results.get(results.size()-a+i));
        }

        if (HasEmptyResult(items))
        {
            return RuleStatement.DISABLE;
        }

        //var hs = items.Where(p => p.Z >  L).ToList();
        List<Result> hs = items.stream().filter(p -> p.getZvalue() > l).collect(Collectors.toList());
        //var ls = items.Where(p => p.Z <  L * -1).ToList();
        List<Result> ls = items.stream().filter(p -> p.getZvalue() < l * -1).collect(Collectors.toList());

        if (hs.size() >= a || ls.size() >= a)
        {

            items.get(items.size()-1).setIsOutControl(true);

            items.get(items.size()-1).getOutControlInfos().add(new OutControlInfo(id,false,
                    hs.size()>=a?hs.stream().map(Result::getId).collect(Collectors.toList()) : ls.stream().map(Result::getId).collect(Collectors.toList())));
//            {
//                RuleId = Id,
//                IsMutiMaterial = false,
//                MemberIds = hs.Count() >= A ? hs.Select(p => p.Id).ToList() : ls.Select(p => p.Id).ToList()
//            });

            return RuleStatement.OK;
        }


        return RuleStatement.FAILED;
    }

    @Override
    public QCResult ListToQCResult(List<QCResult> results) {


        if (CollectionUtils.isEmpty(results))
        {
            return null;
        }




        QCResult result =new QCResult();
        //两个质控物,41s
        if (a > results.size())
        {

            //不能整除,失败
            if (a % results.size() > 0)
            {
                return null;
            }

            int count = a / results.size();

            //获取必要数据



            for (QCResult r : results)
            {
                if(r.getResults().size() < count)
                {
                    return null;
                }

                //result.getResults().AddRange(r.Results.GetRange(r.Results.Count() - count, count).ShallowClone());

                List<Result> res = r.getResults();
                //var items = result.Results.GetRange(result.Results.Count - A, A);
                List<Result> items = new ArrayList<>();
                for(int i = 0 ;i< count; i++){
                    items.add(res.get(res.size()-count+i));
                }
                result.getResults().addAll(items);
            }

        }
        //2个质控物,22s;31s
        //三个质控物 22s无法解决
        else
        {
            for (QCResult r : results)
            {
                if (r.getResults().size() < 1)
                {
                    return null;
                }

                result.getResults().add(r.getResults().get(r.getResults().size()-1));
            }
        }

        return result;
    }
}
